using System;
using Microsoft.Win32;
using System.IO;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Reflection;
namespace BTSProjectWizard
{
    public class ConfigBase
    {

        private XmlAttribute[] _attributes;

        [XmlAnyAttribute]
        public XmlAttribute[] Attributes
        {
            get { return _attributes; }
            set { _attributes = value; }
        }

    }
    public abstract class PathBase : ConfigBase
    {
        public string Path
        {
            get { return _Path; }
            set { _Path = value; }
        }
        string _Path;
    }

    public class Reference : PathBase
    {

    }
    public class ReferenceCollection : List<Reference>
    {
    }
    public class VariableCollection : List<BTSProjectWizard.Variable>
    {
    }
    public enum DirectionType
    {
        In,
        Out
    }
    public class Variable
    {
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        string _Name;
        public string Type
        {
            get { return _Type; }
            set { _Type = value; }
        }
        string _Type;
        public DirectionType Direction
        {
            get { return _Direction; }
            set { _Direction = value; }
        }

        DirectionType _Direction;
    }
    public class WizardPageTypeConverter : TypeConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            StringCollection ret = DesignHelper.GetTypeCollectionByBase(typeof(BTSWizardPage));
            return new TypeConverter.StandardValuesCollection(ret);
        }
    }
    internal static class DesignHelper
    {
        static internal StringCollection GetTypeCollectionByBase(Type baseType)
        {
            StringCollection ret = new StringCollection();


            foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type t in a.GetTypes())
                {
                    if (t.IsSubclassOf(baseType))
                        ret.Add(t.AssemblyQualifiedName);
                }

            }
            return ret;
        }
        static internal StringCollection GetTypeCollectionByItf(Type interfacetype)
        {
            StringCollection ret = new StringCollection();

            Type i = null;
            foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type t in a.GetTypes())
                {
                    i = t.GetInterface(interfacetype.FullName);
                    if (i != null)
                    {
                        ret.Add(t.AssemblyQualifiedName);
                    }
                    else
                    {
                        i = null;
                    }
                }

            }
            return ret;
        }
    }
    public class WizardFileTypeConverter : TypeConverter
    {
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            StringCollection ret = DesignHelper.GetTypeCollectionByItf(typeof(IBTWizardFileGen));
            return new TypeConverter.StandardValuesCollection(ret);
        }
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
    public class WizardPage : ConfigBase
    {

        private string _typeName;

        [TypeConverter(typeof(WizardPageTypeConverter))]
        public string TypeName
        {
            get { return _typeName; }
            set { _typeName = value; }
        }

        private string _title;

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private string _data;

        public string Data
        {
            get { return _data; }
            set { _data = value; }
        }

    }
    public class WizardPageCollection : List<WizardPage>
    {
    }
    public class WizardFile : PathBase
    {

        public WizardFile()
        {
        }
        public WizardFile(string type, string path)
        {
            this.TypeName = type;
            this.Path = path;
        }

        private string _type;
        [XmlAttribute(AttributeName = "Type")]
        [TypeConverter(typeof(WizardFileTypeConverter))]
        public string TypeName
        {
            get { return _type; }
            set { _type = value; }
        }

    }
    public class WizardFileCollection : List<WizardFile>
    {
    }

    public class WizardInfo
    {
        [XmlIgnore()]
        public string ShortName;
        private string _wizardName;
        [XmlElement("Name")]
        public string WizardName
        {
            get { return _wizardName; }
            set { _wizardName = value; }
        }
        [Description("References to .NET assemblies that will be added to the generated project")]
        public ReferenceCollection References
        {
            get { return _References; }
            set { _References = value; }
        }
        ReferenceCollection _References = new ReferenceCollection();
        [Description("The set of Controls that will make up the 'pages' of the wizard")]
        public WizardPageCollection Pages
        {
            get { return _Pages; }
            set { _Pages = value; }
        }
        WizardPageCollection _Pages = new WizardPageCollection();
        [Description("The path to the project file that will be used as the template for the generated project")]
        public string ProjectFile
        {
            get { return _ProjectFile; }
            set { _ProjectFile = value; }
        }
        string _ProjectFile;

        WizardFileCollection _WizardFiles = new WizardFileCollection();
        [Description("The files that will be added to the generated project")]
        public WizardFileCollection WizardFiles
        {
            get { return _WizardFiles; }
            set { _WizardFiles = value; }

        }

    }
    /// <summary>
    /// Summary description for Util.
    /// </summary>
    public class Util
    {
        private Util() { }
        public static string GetBizTalkInstallProductLanguage()
        {
            RegistryKey key1 = Registry.LocalMachine;
            string text1 = "";
            RegistryKey key2 =
 key1.OpenSubKey(@"SOFTWARE\Microsoft\BizTalk Server\3.0");
            text1 = key2.GetValue("ProductLanguage").ToString();
            return text1;

        }
        public static string GetBizTalkInstallPath()
        {
            RegistryKey key1 = Registry.LocalMachine;
            string text1 = "";
            RegistryKey key2 = key1.OpenSubKey(@"SOFTWARE\Microsoft\BizTalk Server\3.0");
            text1 = key2.GetValue("InstallPath").ToString();
            return text1;
        }
        public static string GetVSDirDir()
        {
            string path = Path.Combine(Util.GetBizTalkInstallPath(), _vsdir);
            return path;
        }
        static string _vsdir = @"Developer Tools\BizTalkProjects\Pattern Templates";
        public static string GetWizardDir()
        {
            string path = Path.Combine(Util.GetBizTalkInstallPath(), _wizarddir);
            return path;
        }
        static string _wizarddir = @"Developer Tools\BizTalkWizards\";
        public static WizardInfo LoadFiles(string path)
        {
            WizardInfo wi = Deserialize(path);
            return wi;
        }
        //static WizardInfoSerializer _wis = new WizardInfoSerializer();
        static XmlSerializer _wis = new XmlSerializer(typeof(WizardInfo));
        public static WizardInfo Deserialize(string path)
        {
            FileStream fs = File.OpenRead(path);
            return (WizardInfo)_wis.Deserialize(fs);
        }
        public static void Serialize(WizardInfo wi, string path)
        {
            FileStream fs = File.Open(path, FileMode.Create);
            _wis.Serialize(fs, wi);
            fs.Close();
        }

    }
}
